var username=getCookie("username")
var into1=document.querySelector('.into1  a')
var into=document.querySelector('.into1 ')
var into2=document.querySelector('.into2  a')
var pic =document.querySelector('.pic')
console.log(username)
if(username){
    console.log(into1)
    into.style.marginLeft='165px'
    into2.innerHTML=` <a href="javascript:;" class="logout">退出</a>`
    into1.innerHTML=`<a href="javascript:;" style="color:red;">${username}</a>`
    into2.querySelector('a.logout').onclick = function(){
        if(!confirm('你确定要离开吗？')){
            return false
        }
        removeCookie('username')
        into.style.marginLeft='219px'
        into1.innerHTML=`<a href="login.html">登录</a>`
        into2.innerHTML = `<a href="register.html">注册</a>`
    }
}

$.ajax({
    url:'./php/list.php',
    method:'get',
    data:{},
    dataType:'json',
    success(res){
        // console.log(res)
        // var str ='';
        // res.data.forEach(item => {
        //     str +=`<a href="detail.html"><img src="${item.img}">
        //     <h4>${item.name} </h4>
        //     <p>¥${item.price}.00 <b>多款可选</b></p>
        //     <p>赠送积分</p>
        //     <p>6490人评价&nbsp;&nbsp;99%好评</p>
        //     </a>`
        // });
        // pic.innerHTML =str;
        
        layui.use(['laypage', 'layer'], function(){
            var laypage = layui.laypage
            ,layer = layui.layer;
            laypage.render({
                elem: 'demo20'
                ,limit:5
                ,count: res.data.length
                ,limits:[5,10,15]
                ,layout:['count', 'prev', 'page', 'next', 'limit',  'skip']
                ,jump: function(obj){
                  //模拟渲染
                  pic.innerHTML = function(){
                    var arr = []
                    ,thisData = res.data.concat().splice(obj.curr*obj.limit - obj.limit, obj.limit);
                    layui.each(thisData, function(index, item){
                      arr.push(`<a href="detail.html?${item.id}"><img src="${item.img}">
                      <h4>${item.name} </h4>
                      <p>¥${item.price}.00 <b>多款可选</b></p>
                      <p>赠送积分</p>
                      <p>6490人评价&nbsp;&nbsp;99%好评</p>
                      </a>`);
                    });
                    return arr.join('');
                  }();
                }
              });
              
            });
    }
})